let daoUtil = require('./daoUtil');
let crypto=require("crypto");
let isNull = daoUtil.isNull;
let jsonWrite = daoUtil.jsonWrite;
let procQueryColum = daoUtil.procQueryColum;
let getDBName = daoUtil.getDBName;
let $conf = require('../conf/commonConf');

var path = require('path');

// 此处的数据库名称必须要与之前配置的名称一致
// 与
// tribe_config.db('antifraud_wansheng',$conf.mysql_wansheng);
// tribe_config.db('antifraud',$conf.mysql_weishang);
// 这两条语句中配置的名称一致
let db_local=$conf.mysql_database.database;

let debug = false;

let $fs = require('fs');

let conndb = require('./conndb');
let tribe_pool = conndb.tribe_pool;

let queryPersonnalData = function(req,res,next){
    if(debug){
        $fs.readFile(__dirname+'/IndexData.json',function(err,data){
            jsonWrite(res,JSON.parse(data));
        })
        return;
    }
    let param=req.body;
    let queryColum=procQueryColum(param);
   
    // let querystring=`select ${queryColum} from demo_table`;
    let querystring=`select * from demo_table`;
    
    tribe_pool.query(querystring,{table:'demo_table',db:db_local},function(err,rows,fields){
        if(err){
            process.stdout.write((new Date()).toISOString()+"^"+err.stack+"\n");
            jsonWrite(res, []);
        }
        jsonWrite(res, rows);
    });
}

let insertPersonalData = function(req,res,next){
    if(debug){
        $fs.readFile(__dirname+'/IndexData.json',function(err,data){
            jsonWrite(res,JSON.parse(data));
        })
        return;
    }
    let param=req.body;
    let queryColum=procQueryColum(param);
    let querystring=`insert into demo_table VALUES('${param.guid}','${param.name}','${param.gender}',${param.age},'${param.describe}');`;
    
    tribe_pool.query(querystring,{table:'demo_table',db:db_local},function(err,rows,fields){
        if(err){
            process.stdout.write((new Date()).toISOString()+"^"+err.stack+"\n");
            jsonWrite(res, {
                code:500,
                message:JSON.stringify(err.stack)
            });
        }
        jsonWrite(res, {
            code:200,
            message:"插入成功"
        });
    });
}

let insertDangersAlertData = function(req,res,next){
    if(debug){
        $fs.readFile(__dirname+'/IndexData.json',function(err,data){
            jsonWrite(res,JSON.parse(data));
        })
        return;
    }
    let param=req.body;
    let queryColum=procQueryColum(param);

    let querystring=`insert into dangers_alert VALUES('${param.guid}','${param.name}','${param.time}','${param.status}','${param.image_name}','${param.gender}',${param.age},'${param.describe}');`;
    process.stdout.write(querystring);
    $fs.writeFileSync(path.join(__dirname,`../public/dest/images/${param.image_name}`), new Buffer(param.image, 'base64'))

    tribe_pool.query(querystring,{table:'dangers_alert',db:db_local},function(err,rows,fields){
        if(err){
            process.stdout.write((new Date()).toISOString()+"^"+err.stack+"\n");
            jsonWrite(res, {
                code:500,
                message:JSON.stringify(err.stack)
            });
        }
        jsonWrite(res, {
            code:200,
            message:"插入成功"
        });
    });
}

let queryDangersAlertData = function(req,res,next){
    if(debug){
        $fs.readFile(__dirname+'/IndexData.json',function(err,data){
            jsonWrite(res,JSON.parse(data));
        })
        return;
    }
    let param=req.body;
    let queryColum=procQueryColum(param);
    let querystring=`select * from dangers_alert`;

    tribe_pool.query(querystring,{table:'dangers_alert',db:db_local},function(err,rows,fields){
        if(err){
            process.stdout.write((new Date()).toISOString()+"^"+err.stack+"\n");
            jsonWrite(res, []);
        }
        jsonWrite(res, rows);
    });
}

module.exports = {
    queryPersonnalData:queryPersonnalData,    
    insertPersonalData:insertPersonalData,
    insertDangersAlertData:insertDangersAlertData,
    queryDangersAlertData:queryDangersAlertData
};
